import { onUnmounted } from 'vue'
import { uuid } from '@/utils/cloud/utils'
import { useCloudStore } from '@/store/modules/cloud/index'

export default () => {
  const store = useCloudStore()

  let uuidList = []
  const getUUID = () => {
    const id = uuid()
    uuidList.push(id)
    return id
  }

  const clear = () => {
    return new Promise((resolve, reject) => {
      store.getSocket.then(socket => {
        if (socket) {
          socket.onUnmoun(uuidList)
          uuidList = []
        }

        resolve()
      })
    })
    return true
  }

  onUnmounted(() => {
    //卸载
    if (uuidList.length) {
      store.getSocket.then(socket => {
        if (socket) {
          socket.onUnmoun(uuidList)
        }
      })
    }
  })

  return { getUUID, store, clear }
}
